<!DOCTYPE html>
<meta charset="UTF-8">
<html xmlns:th="http://www.thymeleaf.org" xmlns:ewsd="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
      layout:decorator="public/layout">

<head>
    <title>微应用Demo</title>
    <!-- 这个必须引入的啊，钉钉的前端js SDK, 使用框架的请自行参照开发文档 -->
    <script src="https://g.alicdn.com/dingding/dingtalk-jsapi/2.7.13/dingtalk.open.js"></script>
    <!-- 这个jquery 想不想引入自己决定，没什么影响 -->

    <script type="text/javascript" src="/jquery/jquery.min.js"></script>
    <script type="text/javascript" src="/jquery/jquery.cookie.js"></script>

    <!--<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.slim.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.slim.min.js"></script>-->

</head>

<body>
<th:block layout:fragment="content">

    <hr>
    <h1>微应用Demo</h1>
    <p>当前页面的url:</p>
    <p id="url"></p>
    <br>
    <p>解析url,获取的corpID:</p>
    <p id="corpId"></p>
    <br>
    <p>SDK初始化获取的code:</p>
    <p id="code"></p>
    <br>
    <p>请求我们服务端,登录返回的结果:</p>
    <p id="result"></p>

</th:block>

<th:block layout:fragment="js">

    <script type="text/javascript">


        $(function () {
            //let k  = ${corpid}
            //钉钉sdk 初始化
            // dd.ready参数为回调函数，在环境准备就绪时触发，jsapi的调用需要保证在该回调函数触发后调用，否则无效。
            dd.ready(function () {
                //获取当前网页的url
                //http://ding-web.lnexin.cn/?corpid=ding46a9582af5b7541b35c2fxxxxxxxxxx8f
                var currentUrl = document.location.toString()
                $("#url").append(currentUrl)

                // 解析url中包含的corpId
                //var corpId = currentUrl.split("corpid=")[1];
                var corpId = "dingbf9bec964f0fb52a24f2f5cc6abecb85"
                $("#corpId").append(corpId)

                //使用SDK 获取免登授权码
                dd.runtime.permission.requestAuthCode({
                    corpId: corpId,
                    onSuccess: function (result) {
                        var code = result.code;
                        $("#code").append(code)
                        //请求我们服务端的登陆地址
                        $.ajax({
                            url: "http://ding.chenyc.club/login?authCode="+code,
                            method:"POST",
                            success: function (result) {
                                result.result.name;
                                for (item in response) {
                                    $("#result").append("<li>" + item + ":" + response[item] + "</li>")
                                }
                                if (response.user) {
                                    //sessionStorage.setItem("userInfo",response.user);
                                    for (item in response.user) {
                                        $("#result").append("<li>\t[user 属性] " + item + " : " + response.user[item] + "</li>")
                                    }
                                }
                            }
                        });
                    }
                });
            });
        })

    </script>
</th:block>
</body>
</html>
